from fastapi import APIRouter, Depends, Request

from forward.api.pc.schemas.article import ArticleSearchOutSchema
from forward.api.pc.schemas.index import (
    PolicyInSchema,
    DecorateInSchema,
    SearchInSchema,
)
from forward.api.pc.service.index import IndexService, IndexService
from forward.http_base import unified_resp
from forward.schema_base import PageInationResult

router = APIRouter()


@router.get("/index")
@unified_resp
async def index(index_service: IndexService = Depends(IndexService.instance)):
    return await index_service.index()


@router.get("/decorate")
@unified_resp
async def decorate(
    decorate_in: DecorateInSchema = Depends(),
    index_service: IndexService = Depends(IndexService.instance),
):
    return await index_service.decorate(decorate_in.id)


@router.get("/config")
@unified_resp
async def config(
    request: Request, index_service: IndexService = Depends(IndexService.instance)
):
    domain = (
        f"{request.url.hostname}:{request.url.port}"
        if request.url.port
        else request.url.hostname
    )
    print("domian", request.url)
    return await index_service.config(domain)


@router.get("/policy")
@unified_resp
async def policy(
    policy_in: PolicyInSchema = Depends(),
    index_service: IndexService = Depends(IndexService.instance),
):
    """协议"""
    return await index_service.policy(policy_in)


@router.get("/hotSearch")
@unified_resp
async def hot_search(index_service: IndexService = Depends(IndexService.instance)):
    """热搜"""
    return await index_service.hot_search()


@router.get("/search", response_model=PageInationResult[ArticleSearchOutSchema])
@unified_resp
async def search(
    search_in: SearchInSchema = Depends(),
    index_service: IndexService = Depends(IndexService.instance),
):
    """搜索"""
    return await index_service.search(search_in)
